package com.project.card;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class BusinessActivity extends AppCompatActivity {
    private Dialog dialog;
    private Button btn_agree;
    private Button btn_disagree;
    private Button btn_apply;
    private Button btn_check;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_business);



        View view =LayoutInflater.from(BusinessActivity.this).inflate((R.layout.dialog_privacy_show),null);
        btn_apply = findViewById(R.id.business_apply);
        btn_agree=(Button) view.findViewById(R.id.privacy_btn_agree);
        btn_disagree=(Button) view.findViewById(R.id.privacy_btn_disagree);
        btn_check = findViewById(R.id.business_check);
        btn_apply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPrivacy("privacy.txt");
            }
        });
        btn_agree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                getSharedPreferences("file", Context.MODE_PRIVATE).edit().putBoolean("AGREE",true).apply();
            }
        });
        btn_disagree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
//                Intent intent=new Intent(BusinessActivity.this, MainActivity.class);
//                startActivity(intent);
            }
        });
        btn_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog alertDialog = new AlertDialog.Builder(BusinessActivity.this)
                        .setTitle("当前办理进度")
                        .setMessage("")
                        .setPositiveButton("确定",new DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        }).create();
                alertDialog.show();
            }
        });
    }


    public void showPrivacy(String privacyFileName){
        String str = initAssets(privacyFileName);
        final View inflate = LayoutInflater.from(BusinessActivity.this).inflate(R.layout.dialog_privacy_show,null);
        //TextView tv_title = (TextView) inflate.findViewById(R.id.tv_title);
        //tv_title.setText();
        TextView content = (TextView) inflate.findViewById(R.id.privacy_tv_content);
        content.setText(str);
        Button btnAgree = inflate.findViewById(R.id.privacy_btn_agree);
        Button btnDisagree = inflate.findViewById(R.id.privacy_btn_disagree);
        btnAgree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                Intent intent = new Intent(BusinessActivity.this,InfoActivity.class);
                startActivity(intent);
            }
        });

        btnDisagree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        dialog = new AlertDialog.Builder(BusinessActivity.this).setView(inflate).show();

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = dm.widthPixels*4/5;
        params.height = dm.heightPixels/2;
        dialog.setCancelable(false);
        dialog.getWindow().setAttributes(params);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    }

    public String initAssets(String fileName){
        String str=null;
        try{
            InputStream inputStream = getAssets().open(fileName);
            str=getString(inputStream);
        }catch (IOException ioException){
            ioException.printStackTrace();
        }
        return str;
    }

    public static String getString(InputStream inputStream){
        InputStreamReader inputStreamReader = null;
        try{
            inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
        }catch (UnsupportedEncodingException unsupportedEncodingException){
            unsupportedEncodingException.printStackTrace();
        }
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        StringBuffer sb = new StringBuffer("");
        String line;
        try{
            while((line = bufferedReader.readLine())!=null){
                sb.append(line);
                sb.append("\n");
            }
        }catch (IOException ioException){
            ioException.printStackTrace();
        }
        return sb.toString();
    }
}